<?php
class Plugins_HttpConditional extends Zend_Controller_Plugin_Abstract
{
	 
	public function dispatchLoopShutdown()
	{
	    
	    
		if(!isset($_GET['debug']) && $this->getRequest()->getParams('NOETAG') == ""){
		$send_body = true;
		 
		$etag = '"' . md5($this->getResponse()->getBody()) . '"';
		 
		$inm = split(',', getenv("HTTP_IF_NONE_MATCH"));
		 
		$inm = str_replace('-gzip', '', $inm);

		// TODO If the request would, without the If-None-Match header field,
		// result in anything other than a 2xx or 304 status,
		// then the If-None-Match header MUST be ignored
		
		foreach ($inm as $i) {
			if (trim($i) == $etag) {
				$this->getResponse()
				->clearAllHeaders()
				->setHttpResponseCode(304)
				->clearBody();
				$send_body = false;
				break;
			}
		}
		 
		$this->getResponse()
		->setHeader('Cache-Control', 'max-age=7200, must-revalidate', true)
		->setHeader('Expires', gmdate('D, d M Y H:i:s', time() + 2 * 3600) . ' GMT', true)
		->clearRawHeaders();
		 
		if ($send_body) {
			$this->getResponse()
			->setHeader('Content-Length', strlen($this->getResponse()->getBody()));
		}
		$this->getResponse()->setHeader('ETag', $etag, true);
		$this->getResponse()->setHeader('Pragma', '');
		}
	}
}